package com.costeaalex.blueserver.server;

import java.util.ArrayList;

public class ThreadManager
	{
	private ArrayList<BlueServerSenderThread> threads;
	private ArrayList<ThreadControl> controls;
	private int currentIndex=0;
	private boolean canRotate;
	
	public ThreadManager()
		{
		threads = new ArrayList<BlueServerSenderThread>();
		controls = new ArrayList<ThreadControl>();
		canRotate=true;
		}
	
	public boolean isCanRotate()
		{
		return canRotate;
		}

	public void setCanRotate(boolean canRotate)
		{
		this.canRotate = canRotate;
		}

	public synchronized void rotate(BlueServerSenderThread currentThread)
		{
		if(canRotate)
			{
			currentIndex=threads.indexOf(currentThread);
	
			if(currentIndex<threads.size()-1)
				{
				controls.get(currentIndex+1).unblockThread();
				currentIndex++;
				}
			else
				{
				controls.get(0).unblockThread();
				currentIndex=0;
				}
			}
		}
	
	public synchronized void rotate()
		{

		if(currentIndex<threads.size()-1)
			{
			currentIndex++;
			controls.get(currentIndex).unblockThread();
			}
		else
			{
			controls.get(0).unblockThread();
			currentIndex=0;
			}
		}
	
	public synchronized void start()
		{
		System.out.println("Start");
		controls.get(0).unblockThread();
		}
	
	public synchronized void blockCurrent()
		{
		controls.get(currentIndex).blockThread();
		}
	
	public synchronized void unblock(BlueServerSenderThread t)
		{
		controls.get(threads.indexOf(t)).notifyAll();
		}
	
	public synchronized void push(BlueServerSenderThread t, ThreadControl tc)
		{
		threads.add(t);
		controls.add(tc);
		}
	
	public synchronized void destroy(BlueServerSenderThread t)
		{
		threads.get(threads.indexOf(t)).destroy();
		}
	
	public synchronized void activate(BlueServerSenderThread t)
		{
		threads.get(threads.indexOf(t)).activate();
		}
	
	public synchronized void deActivate(BlueServerSenderThread t)
		{
		threads.get(threads.indexOf(t)).deActivate();
		}
	
	public boolean isAlive(BlueServerSenderThread t)
		{
		return false;
		}
	
	public boolean isActive(BlueServerSenderThread t)
		{
		return false;
		}
	}
